<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/23
 * Time: 10:11
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Interest extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            
            $count = Db::table('up_interest')->count();
            $lists = Db::table('up_interest')
                ->field('interest_id, interest_word, interest_time, interest_update_time')
                ->order('interest_update_time desc')
                ->page($Nowpage, $limits)
                ->select();//计算总页面
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        
        return $this->fetch();
    }
    
    /**
     * 添加兴趣标签
     * @return mixed|\think\response\Json
     */
    public function interestAdd(){
        if(request()->isPost()){
            $interest_word = trim(input('interest_word'), ' ');
            $isHave = Db::table('up_interest')->where('interest_word', $interest_word)->find();
            if($isHave){
                return json(['code' => 100, 'msg' => '该标签已存在']);
            }
            $data['interest_word'] = $interest_word;
            $data['interest_time'] = date('Y-m-d H:i:s');
            $data['interest_update_time'] = date('Y-m-d H:i:s');
            $res = Db::table('up_interest')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('interest/interestadd');
    }
    
    /**
     * 编辑兴趣标签
     * @return mixed|\think\response\Json
     */
    public function interestEdit(){
        if(Request::instance()->isPost()){
            $interest_word = input('interest_word');
            $isHave = Db::table('up_interest')->where('interest_word', $interest_word)->find();
            if($isHave){
                return json(['code' => 100, 'msg' => '该标签已存在']);
            }
            $id = input('interest_id');
            $res = Db::table('up_interest')->where('interest_id', $id)
                ->update([
                    'interest_word' => $interest_word,
                    'interest_update_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $interest_id = input('interest_id');
        $interest_word = Db::table('up_interest')->where('interest_id', $interest_id)->value('interest_word');
        $this->assign([
            'interest_id' => $interest_id,
            'interest_word' => $interest_word
        ]);
        return $this->fetch('interest/interestedit');
    }
    
    /**
     * 删除兴趣标签
     * @return \think\response\Json
     */
    public function interestDel(){
        $interest_id = input('id');
        $res = Db::table('up_interest')->delete($interest_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}